<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Log;
use App\Models\AdminUser;
use App\Models\AdminRole;
use Illuminate\Support\Facades\DB;

class LoginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    /**
     * 登录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function index()
    {
        //
        return view('login.index');
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 后台权限管理主页面
     */
    public function home()
    {
        return view('home.index');
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 用户管理
     */
    public function user()
    {
        return view('user.user');
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 角色管理
     */
    public function role()
    {
        return view('role.role');
    }

    //验证登录
    public function login()
    {

        $user = request(['name','password']);

        $is_remember = boolval(request('is_remember'));
        if ( Auth::attempt($user, $is_remember) )
        {
            return redirect(route('home.index'));
        } else {
            return Redirect::back()->withErrors('账号密码不匹配');
        }
    }




    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 添加用户页面
     */
    public function createuser()
    {
        //
        return view('create.createuser');
    }
    //添加用户
    public function ucenter(Request $request){
//        $input = $request->all();
        $name = $request->input('name');
        $password = $request->input('password');
        $email = $request->input('email');
        $realname = $request->input('realname');

        $res = AdminUser::create(compact('name','password','email', 'realname'));
        if($res){
            return redirect(route('admin.user'));
        }

    }
    //用户展示页面
    public function show(){
        $res = DB::table('users')->get();
        $data['code'] = 0;
        $data['msg'] = 'success';
        $data['count'] = DB::table('users')->count();
        $data['data'] = $res;
        return json_encode($data);

    }
    //添加角色页面
    public function createrole()
    {
        //
        return view('create.createrole');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     * 添加用户
     */
    public function store(Request $request)
    {
        //
        AdminUser::create(request(['name','realname']));
        return redirect(route('role.show'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     * 删除方法
     */
    public function deluser(Request $request)
    {
        //
        $ji = $request->input('id');

        if(AdminUser::destroy($ji)){
            $data['code'] = 200;
            $data['msg'] = 'success';
        }else{
            $data['code'] = 400;
            $data['msg'] = 'fail';
        }

        return response()->json($data);
    }
    //获取修改
    public function edit($id){
        $res = DB::table('users')->where('id',$id)->first();
//        dd($res);
//        exit();
        return view('create.updateuser',compact('res'));

    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     * 修改方法
     */
    public function update(Request $request){
        $cut = $request->all();
        $sql = AdminUser::where('id',$cut['id'])->update(['name'=>$cut['name'],
            'password' => $cut['password'],
            'email' => $cut['email'],
            'realname' => $cut['realname']]);

        if($sql){
            return redirect(route('admin.user'));
        }
    }

    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 跳转分配角色
     */
    public function userrole($id){

        return view('givepromission.usro');
    }


    /**
     * @param AdminUser $user
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 展示接受的角色信息
     */
    public function getsrole(AdminUser $user)
    {
        $user = Auth::user();
        $roles = AdminRole::all();
        $myRoles = $user->roles()->get();
        return view('givepromission.usro', compact('user','roles','myRoles'));

    }

    /**
     * @param AdminUser $user
     * @return \Illuminate\Http\RedirectResponse
     * 执行角色分配
     */
    public function showrole(AdminUser $user){
//        $this->validate(request(),[
//            'roles' =>'array'
//        ]);
        $user = Auth::user();
        $roles = AdminRole::findMany(request('roles'));
        $myRoles = $user->roles()->get();

        //要增加的角色
        $addRoles = $roles->diff($myRoles);
        foreach($addRoles as $role) {
            $user->assignRole($role);
        }

        //要删除的
        $deleteRoles = $myRoles->diff($roles);
        foreach($deleteRoles as $role) {
            $user->deleteRole($role);
        }
        return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
}